import { PureComponent } from 'react';
import WPT from 'wpt';

export default class LoadableLoading extends PureComponent {
    componentDidMount() {
        this.checkLoaidng(this.props);
    }

    componentWillReceiveProps(nextProps) {
        this.checkLoaidng(nextProps);
    }

    componentWillUnmount() {
        WPT.Modal.loading.close();
    }

    checkLoaidng = props => {
        const {
            isLoading, timedOut, error, pastDelay
        } = props;

        // 请求超时或者出现错误
        if (isLoading && timedOut) {
            WPT.Modal.alert('网络不给力，请重试或联系客服');

            return;
        }

        // 出现错误
        if (!isLoading && error) {
            console.error(error);
            WPT.Modal.alert('出错啦，请重试');

            return;
        }

        // 加载中
        if (isLoading && pastDelay) {
            WPT.Modal.loading('加载中...');
            return;
        }

        WPT.Modal.loading.close();
    };

    render() {
        return null;
    }
}
